import { Controller, Post, Body, UsePipes, Get, Query, Inject, ValidationPipe } from '@nestjs/common';
import { UserService } from '../../services/admin/user.service';
import { CreateUserDto } from '../../dto/create-user.dto';
import type { ILanguageService } from '@wwjCommon/language/language.interface';

/**
 * 用户控制器
 * 基于PHP的验证器分组方式，符合NestJS规范
 * 
 * 路由前缀: /adminapi/user
 * 验证器分组: validate_user
 */
@Controller('adminapi/user')
export class UserController {
  constructor(
    private readonly userService: UserService,
    @Inject('ILanguageService') private readonly languageService: ILanguageService,
  ) {}

  /**
   * 创建用户
   * 使用标准NestJS验证管道
   */
  @Post('create')
  @UsePipes(ValidationPipe)
  async create(@Body() createUserDto: CreateUserDto) {
    try {
      const result = await this.userService.create(createUserDto);
      
      // 使用多语言API消息
      const message = await this.languageService.getApiMessage('add_success');
      
      return {
        code: 200,
        message,
        data: result,
      };
    } catch (error) {
      const message = await this.languageService.getApiMessage('add_fail');
      
      return {
        code: 500,
        message,
        error: error.message,
      };
    }
  }

  /**
   * 获取用户列表
   * 使用多语言字典数据
   */
  @Get('list')
  async getList(@Query() query: any) {
    try {
      const result = await this.userService.getList(query);
      
      // 使用多语言字典数据
      const statusDict = await this.languageService.getDictData('dict_user.status_on');
      const typeDict = await this.languageService.getDictData('dict_user.type_admin');
      
      return {
        code: 200,
        message: await this.languageService.getApiMessage('success'),
        data: {
          ...result,
          dict: {
            status: statusDict,
            type: typeDict,
          },
        },
      };
    } catch (error) {
      const message = await this.languageService.getApiMessage('data_get_fail');
      
      return {
        code: 500,
        message,
        error: error.message,
      };
    }
  }

  /**
   * 获取用户详情
   */
  @Get('detail')
  async getDetail(@Query('id') id: string) {
    try {
      const result = await this.userService.getDetail(id);
      
      if (!result) {
        const message = await this.languageService.getApiMessage('data_not_exist');
        return {
          code: 404,
          message,
        };
      }
      
      return {
        code: 200,
        message: await this.languageService.getApiMessage('success'),
        data: result,
      };
    } catch (error) {
      const message = await this.languageService.getApiMessage('data_get_fail');
      
      return {
        code: 500,
        message,
        error: error.message,
      };
    }
  }
}
